/******************************************************************************
* aggregate results and show place effects from two-way fixed effect model (Table A5)
*****************************************************************************/

use "$dataRoot/TU_place_vs_customer_cz${ext}_v1_bs_wtd.dta", clear

merge 1:1 cz using "${dataRoot}/tu_cz_NvS.dta"
gen     indNvS=0 if NvS==1
replace indNvS=1 if NvS==0

keep if _merge==3
drop _merge


capture program drop placeVperson
program define placeVperson
	args CZhigh CZlow var id setInd indVar
	
	preserve
	
	if `setInd'==1{
		qui gen     ind_ = 1 if `var'>= ${`var'_`CZhigh'}
		qui replace ind_ = 0 if `var'<= ${`var'_`CZlow'}
	}
	else{
		qui gen ind_ = `indVar' 
	}

	qui keep if ind_!=.
	
	*collapse cz into these two groups
	qui collapse `var'* [w=numobs], by(ind_) fast

	*difference between groups
	sort ind_
	foreach v of varlist `var'*{
		qui replace `v' = `v'-`v'[_n-1] if _n>1
	}
	qui keep if ind_==1
	
	
	*compute outcome
	qui reshape long `var'_sample `var'_fe_ `var'_p_ , i(ind_) j(sample)

	*place fraction 
	qui gen pl_frac = 100*`var'_p_/`var'_sample	
	qui sum `var'_sample if sample==0
	
	*total difference
	global `var'_`id'_1 = `r(mean)'
	
	*place difference
	qui sum `var'_p_ if sample==0
	global `var'_`id'_2 = `r(mean)'
	
	*person difference
	global `var'_`id'_3 = ${`var'_`id'_1}-${`var'_`id'_2}
	
	*place fraction and SE
	qui sum pl_frac if sample==0
	global `var'_`id'_4 = `r(mean)'
	
	qui sum pl_frac if sample>0
	global `var'_`id'_5 = `r(sd)'
	
	*person fraction
	qui gen pr_frac = 100 - pl_frac	
	qui sum pr_frac if sample==0
	global `var'_`id'_6 = `r(mean)'

	restore
end


***foreach variable
foreach var of varlist $keyMvVars{

	*calcualte cut off to split CZ
	gen `var'_sample0 = `var'
	
	qui sum `var', d
	global `var'_p5  = `r(p5)'
	global `var'_p10 = `r(p10)'
	global `var'_p25 = `r(p25)'
	global `var'_p50 = `r(p50)'
	global `var'_p75 = `r(p75)'
	global `var'_p90 = `r(p90)'
	global `var'_p95 = `r(p95)'		

	
	placeVperson "South" "North"  `var'  1 0	indNvS
	
	
	placeVperson "p50" "p50" `var'  2 1
	placeVperson "p75" "p25" `var'  3 1
	placeVperson "p90" "p10" `var'  4 1
	placeVperson "p95" "p5"  `var'  5 1
	
	
}

******* collect previous results and create tables
*load cut version to get varlist
use "${dataRoot}/tu_mover_delta_zipnodraw_cut.dta", clear

set obs 100
cap drop rank
gen rank=_n

global varFmt   "%12.2fc"


gen vname=""
forval id=1/5{
	gen out_id`id'=""
}

*label variables
labelVars 0 0

local varid = 1
foreach var of varlist $keyMvVars{

	forval id=1/5{

		*label
		qui replace vname = "\addlinespace[0.1em] \\ " + "`:var l `var''" if rank==`varid'
		qui replace vname = "\quad \textit{Overall}"  if rank==(`varid'+1)
		qui replace vname = "\quad \textit{Place}"    if rank==(`varid'+2)
		qui replace vname = "\quad \textit{Person}"   if rank==(`varid'+3)
		qui replace vname = "Share of difference due to"   if rank==(`varid'+4)
		qui replace vname = "\quad \textit{Place}"   if rank==(`varid'+5)
		qui replace vname = "\quad \textit{Person}"    if rank==(`varid'+7)
		
		*overal difference
		qui replace out_id`id'=string(${`var'_`id'_1},        "${varFmt}")      if rank==(`varid'+1)

		*place and person component
		qui replace out_id`id'=string(${`var'_`id'_2},        "${varFmt}")      if rank==(`varid'+2)
		qui replace out_id`id'=string(${`var'_`id'_3},        "${varFmt}")      if rank==(`varid'+3)
		
		
		qui replace out_id`id'=string(${`var'_`id'_4},     "${varFmt}")         if rank==(`varid'+5)
		qui replace out_id`id'="("+string(${`var'_`id'_5}, "${varFmt}")+")"     if rank==(`varid'+6)
		qui replace out_id`id'=string(${`var'_`id'_6},     "${varFmt}")         if rank==(`varid'+7)
		
	}
	
	local varid = `varid'+8
}

drop if vname=="" & out_id1==""

keep  vname out_id*
order vname out_id2-out_id5 out_id1



*** output latex tables (Table A5)
local a "\phantom{xxx}"

local texHead = "\begin{tabular}{ l c c c c c } \hline\hline  " + ///
				"   & \multicolumn{1}{c}{(1)}            & \multicolumn{1}{c}{(2)}          & \multicolumn{1}{c}{(3)}           & \multicolumn{1}{c}{(4)}     		& \multicolumn{1}{c}{(5)}             \\ " + ///
				"   & \multicolumn{1}{c}{`a'Above /`a'}  & \multicolumn{1}{c}{Top \&}       & \multicolumn{1}{c}{Top \&}        & \multicolumn{1}{c}{Top \&}  		& \multicolumn{1}{c}{Deep South}      \\ " + ///
				"   & \multicolumn{1}{c}{below}          & \multicolumn{1}{c}{`a'bottom`a'} & \multicolumn{1}{c}{`a'bottom`a'}  & \multicolumn{1}{c}{`a'bottom`a'}  & \multicolumn{1}{c}{vs}             \\ " + ///
				"   & \multicolumn{1}{c}{median}         & \multicolumn{1}{c}{25\%}         & \multicolumn{1}{c}{10\%}          & \multicolumn{1}{c}{5\%}    		& \multicolumn{1}{c}{Upper Midwest}   \\ \hline "  

				
replace vname =  "\,\, \textit{Share of difference due to place}" if vname[_n-1]=="Share of difference due to" & _n>1
drop if inlist(vname, "\quad \textit{Person}", "Share of difference due to", "\quad \textit{Person}")

replace vname = "\quad Overall" if vname=="\quad \textit{Overall}"
replace vname = "\quad Place"   if vname=="\quad \textit{Place}"


forval t=1/5{
	replace out_id`t' = "\textit{" + out_id`t' + "}" if inlist(vname, "\,\, \textit{Share of difference due to place}","")
}

listtex using "${outDir}/Tex/pl_v_per_decomp_additive_wtd_short_v1.tex" ,replace ///
		rstyle(tabular)	 head("`texHead'") ///
		foot("\hline\hline \end{tabular}")	
		
